﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework;
using Arcadium.Audio;
using Microsoft.Xna.Framework.Input;

namespace Arcadium.GameEntities.Characters
{
    class PlayableCharacter : UpdatableCharacter
    {
        #region Fields

        private NetworkGamer gamer;

        #endregion

        #region Initialization

        public PlayableCharacter(World world, NetworkGamer gamer) : base(world)
        {
            this.gamer = gamer;

            // TODO: Fix spawnpoints.
            Position = world.Map.GetPlayerSpawnPoint(0);
            SetTexture(CharacterType.Player);

            Weapon = new HandGun(world, this);
        }

        #endregion

        #region Update

        /// <summary>
        /// Returns true if dead.
        /// </summary>
        /// <param name="gameTime"></param>
        /// <returns></returns>
        public override bool Update(GameTime gameTime)
        {
            foreach (Crate crate in World.Crates)
            {
                if (GetBoundingRectangle().Intersects(crate.GetBoundingRectangle()))
                {
                    crate.Renew();
                    //World.AddChange(new WorldChange(WorldChange.ChangeType.CrateRelocated, crate.Position));
                }
            }

            // Updates the character and checks if it's dead.
            if (base.Update(gameTime))
            {
                AudioManager.GameOver.Play();
                return true;
            }
            return false;
        }

        /// <summary>
        /// Handles input for this Character.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="playerIndex"></param>
        public void HandleInput(InputState input, PlayerIndex playerIndex)
        {
            // Velocity.X is absolute
            velocity.X = 0;

#if !XBOX
            // Walk
            if (input.IsKeyDown(Keys.Left, playerIndex))
                velocity.X = -Speed;
            if (input.IsKeyDown(Keys.Right, playerIndex))
                velocity.X = Speed;
#endif
            // Walk
            if (input.CurrentGamePadStates[(int)playerIndex].IsConnected)
            {
                float thumbstickX = input.CurrentGamePadStates[
                    (int)playerIndex].ThumbSticks.Left.X;
                velocity.X = thumbstickX * Speed;
            }


            // TODO: Reset position. Debugging purposes.
            if (input.IsKeyDown(Keys.H, playerIndex) || input.IsButtonDown(Buttons.RightStick, playerIndex))
            {
                Position = World.Map.GetPlayerSpawnPoint(0);
                velocity = Vector2.Zero;
            }

            // Jump
            if (OnGround && !IsJumping && (input.IsKeyDown(Keys.Up, playerIndex) ||
                input.IsButtonDown(Buttons.A, playerIndex)))
            {
                StartJumping();
            }
            if (IsJumping && (input.IsKeyUp(Keys.Up, playerIndex) ||
                input.IsButtonUp(Buttons.A, playerIndex)))
            {
                FinalizeJump();
            }

            // Shoot TODO: Fix code. Continual firing etc.
            if (input.IsNewKeyPress(Keys.X, playerIndex)
                || input.IsNewButtonPress(Buttons.LeftTrigger, playerIndex))
            {
                Weapon.Shoot(gamer, Position, Direction);
            }
            if (input.IsNewKeyPress(Keys.RightControl, playerIndex)
                || input.IsNewButtonPress(Buttons.RightTrigger, playerIndex))
            {
                Weapon.Shoot(gamer, Position, Direction);
            }
        }

        #endregion
    }
}
